<?php

/*
|--------------------------------------------------------------------------
| 文件名称:Helper.php
|--------------------------------------------------------------------------
|描述:
|创建人  :dx
|更新人  :dx
|创建时间:16-3-10 下午2:05
|更新时间:14-3-10 下午2:05
*/

class Helper
{
    public static function current_time_string()
    {
        return date("Y-m-d H:i:s", time());
    }

    public static function getImageName()
    {
        return time() . '-' . rand(1, 99999) . '.png';
    }

    public static function timeAfter($type, $value)
    {
        $time = time();
        switch ($type) {
            case 'min':
                $time = $time + $value * 60;
                break;
            case "hour":
                $time = $time + $value * 60 * 60 * 60;
                break;
            case "day":
                $time = $time + $value * 60 * 60 * 60 * 24;
                break;
        }
        return $time;
    }

    /**
     * 获取随机字符串
     * 根据某种算法获取随机字符串，不保证重复
     *
     * @param string $num1 盐值1.
     * @param string $num2 盐值2.
     * @param boolean $num 几位.
     * @return array ['str'=>转换前,'str_num'=>转换后,'char_table'=>['str'=>implode(字母数字表),'arr'=>字母数组表]];.
     * @author dx
     */
    public static  function getStr($num1,$num2,$num = 8){
        $str = '';
        $str = str_pad($num1.$num2,$num,mt_rand(0,9999));
        $char_table = self::getCharTable($str);//获取码表
        $str_num = '';//转换后的字符串
        for ($i=0; $i < strlen($str); $i++) {
            $str_num .= $char_table[$str[$i]];
        }
        return ['str'=>$str,'str_num'=>$str_num,'char_table'=>['str'=>implode($char_table),'arr'=>$char_table]];
    }
    public static function getCharTable($str = ''){
        if(empty($str)) return array();
        $table = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','G','K','L','M','N','P','K','R','S','T','U','V','W','X','Y','Z'];
        $char_table = [];
        for ($i=0; $i < 10; $i++) { //肯定是0-9的数字 所以应该是10位的字母数字表
            $char_table[] = $table[array_rand($table)];
        }
        //防止码表重复，添加缓存
        $cache_key = $str.implode($char_table);
        if(!Cache::has($cache_key)){
            Cache::put($cache_key,1,600);//设置600m过期时间
        }else{
            return self::getCharTable($str);
        }
        return $char_table;
    }

    public static function alphaID($in, $to_num = false, $pad_up = false, $passKey = null)
    {
        $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        if ($passKey !== null) {
            for ($n = 0; $n<strlen($index); $n++) {
                $i[] = substr( $index,$n ,1);
            }

            $passhash = hash('sha256',$passKey);
            $passhash = (strlen($passhash) < strlen($index))
                ? hash('sha512',$passKey)
                : $passhash;

            for ($n=0; $n < strlen($index); $n++) {
                $p[] =  substr($passhash, $n ,1);
            }

            array_multisort($p,  SORT_DESC, $i);
            $index = implode($i);
        }

        $base  = strlen($index);

        if ($to_num) {
            $in  = strrev($in);
            $out = 0;
            $len = strlen($in) - 1;
            for ($t = 0; $t <= $len; $t++) {
                $bcpow = bcpow($base, $len - $t);
                $out   = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
            }

            if (is_numeric($pad_up)) {
                $pad_up--;
                if ($pad_up > 0) {
                    $out -= pow($base, $pad_up);
                }
            }
            $out = sprintf('%F', $out);
            $out = substr($out, 0, strpos($out, '.'));
        } else {
            if (is_numeric($pad_up)) {
                $pad_up--;
                if ($pad_up > 0) {
                    $in += pow($base, $pad_up);
                }
            }

            $out = "";
            for ($t = floor(log($in, $base)); $t >= 0; $t--) {
                $bcp = bcpow($base, $t);
                $a   = floor($in / $bcp) % $base;
                $out = $out . substr($index, $a, 1);
                $in  = $in - ($a * $bcp);
            }
            $out = strrev($out); // reverse
        }

        return $out;
    }

}